'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 设置远征士兵阵容
 * @author agl
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var configUtil = require('../../../../util/configUtil');
var constantUtil = require('../../../../util/constantUtil');
var PlayerManager = require('../../../../libs/playerManager');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var soldierService = require('../../../../services/soldierService.js');
var dataApi = require('../../../../libs/dataApi.js');
var vipConf = require('../../../../config/vipConf.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var soldier_list = msg.soldier_list;

    if (!soldier_list || soldier_list.length < 1) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    var soldier_power = 0;
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['expedition', 'soldier', 'role', 'accrued'], cb);
            },
            function (cb) {
                var expedition_model = player_manager.expedition;
                var role_model = player_manager.role;
                var monster_open_lvl = constantUtil.value('monster_open_lvl');
                if (role_model.lv < monster_open_lvl) {
                    return next(null, {
                        code: Code.USER_LEVEL_NOT_ENOUGH
                    });
                }

                var soldier_model = player_manager.soldier;
                var found_invalid_soldier = false;
                _.forEach(soldier_list, function (soldier_type) {
                    if (soldier_type === 0) {
                        return true;
                    }
                    if (!soldier_model.getSoldier(soldier_type)) {
                        found_invalid_soldier = true;
                        return false;
                    }
                    return true;
                });

                if (found_invalid_soldier) {
                    return next(null, {
                        code: Code.SOLDIER_NOT_EXIST
                    });
                }

                soldier_list = _.slice(soldier_list, 0, 5);
                expedition_model.setSoldierList(soldier_list);
                soldier_power = soldierService.calcSoldierPower(soldier_model, soldier_list);

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    soldier_power: soldier_power
                }
            });
        }
    );
};
